package org.xlx.xlx_oa.meet.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xlx.xlx_oa.common.enums.MeetRoomStatusEnums;
import org.xlx.xlx_oa.meet.convertor.MeetConvertor;
import org.xlx.xlx_oa.meet.domain.MeetFacility;
import org.xlx.xlx_oa.meet.domain.MeetRoom;
import org.xlx.xlx_oa.meet.domain.dto.MeetFacilityDto;
import org.xlx.xlx_oa.meet.domain.dto.MeetRoomDto;
import org.xlx.xlx_oa.meet.domain.vo.MeetFacilityVo;
import org.xlx.xlx_oa.meet.domain.vo.MeetRoomVo;
import org.xlx.xlx_oa.meet.mapper.MeetFacilityMapper;
import org.xlx.xlx_oa.meet.mapper.MeetRoomMapper;

import java.util.List;

@Service
public class MeetRoomService {
    private final MeetConvertor meetConvertor = MeetConvertor.INSTANCE;
    @Autowired
    private MeetRoomMapper roomMapper;
    @Autowired
    private MeetApplyService applyService;
    @Autowired
    private MeetFacilityMapper facilityMapper;

    /**
     * 保存会议室信息
     *
     * @param roomVo
     * @return
     */
    @Transactional
    public Boolean saveMeetRoom(MeetRoomVo roomVo) {
        MeetRoom meetRoom = meetConvertor.convertVoToDomain(roomVo);
        Long id = meetRoom.getId();
        if (id != null) {
            roomMapper.updateById(meetRoom);
        } else {
            meetRoom.setRoomStatus(MeetRoomStatusEnums.NormalUse.name());
            roomMapper.insert(meetRoom);
        }

        UpdateWrapper<MeetFacility> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(MeetFacility::getRoomId, meetRoom.getId());
        facilityMapper.delete(updateWrapper);

        List<MeetFacilityVo> facilityVos = roomVo.getFacilityVos();
        List<MeetFacility> facilities = meetConvertor.convertVoToListMeetFacility(facilityVos);
        for (MeetFacility facility : facilities) {
            facilityMapper.insert(facility);
        }
        return true;
    }

    /**
     * 删除会议室信息
     *
     * @param roomId
     * @return
     */
    @Transactional
    public Boolean removeMeetRoom(Long roomId) {
        roomMapper.deleteById(roomId);
        UpdateWrapper<MeetFacility> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(MeetFacility::getRoomId, roomId);
        facilityMapper.delete(updateWrapper);
        return true;
    }

    /**
     * 获取会议室信息列表
     *
     * @param roomVo
     * @return
     */
    public List<MeetRoomDto> getMeetRoomList(MeetRoomVo roomVo) {
        List<MeetRoom> meetRooms = roomMapper.selectList(null);
        List<MeetRoomDto> meetRoomDtos = meetConvertor.convertToListDtoDomain(meetRooms);
        for (MeetRoomDto meetRoomDto : meetRoomDtos) {
            Boolean busy = applyService.isBusy(meetRoomDto.getId());
            if (busy) {
                meetRoomDto.setNowStatus(MeetRoomStatusEnums.Busy.getText());
            } else {
                meetRoomDto.setNowStatus(MeetRoomStatusEnums.Spare.getText());
            }
        }
        return meetRoomDtos;
    }

    /**
     * 会议室详情
     *
     * @param roomId
     * @return
     */
    public MeetRoomDto getMeetRoom(Long roomId) {
        MeetRoom meetRoom = roomMapper.selectById(roomId);
        MeetRoomDto meetRoomDto = meetConvertor.convertToDtoDomain(meetRoom);
        Boolean busy = applyService.isBusy(meetRoomDto.getId());
        if (busy) {
            meetRoomDto.setNowStatus(MeetRoomStatusEnums.Busy.getText());
        } else {
            meetRoomDto.setNowStatus(MeetRoomStatusEnums.Spare.getText());
        }
        List<MeetFacilityDto> facilities = facilityMapper.selectFacilities(roomId);
        meetRoomDto.setFacilityDtos(facilities);
        return meetRoomDto;
    }
}
